import { Component, OnInit } from '@angular/core';
import { MessageService } from 'primeng/api';
import { BaseComponent, Utils, FileUploadInfo } from 'iisp-ng';

import { DataIEService } from '../dataie.service.js';
import { TableInfo, TableTemplate } from '../model.js';

declare var contextPath: string, fileDownloadRoot: string;

@Component({
	selector: 'data-imp-template',
	templateUrl: './template.component.html',
	providers: [DataIEService]
})
export class ImportTemplateComponent extends BaseComponent implements OnInit {
	tableList: TableInfo[];
	selectedTable: TableInfo;
	tableTemplates: TableTemplate[];
	newTemplate: TableTemplate = new TableTemplate();
	tpltFileUploadInfo: FileUploadInfo = new FileUploadInfo(".xls, .xlsx");
	selectedTemplate: TableTemplate;
	constructor(private dataieService: DataIEService, messageService: MessageService) { 
        super(messageService);
    }

	ngOnInit(): void {
		this.getTableList();
	}

	getTableList(): void {
		this.dataieService.getTableList().subscribe(rst => this.tableList = rst, err => this.handleError(err));
	}

	onSelectTable(tbl: TableInfo): void {
		this.selectedTable = tbl;
		this.newTemplate = new TableTemplate();
		this.newTemplate.tabName = tbl.code;
		for (let tb of this.tableList) {
			tb.isSelected = false;
		}
		this.selectedTable.isSelected = true;
		this.dataieService.getTableTemplateList(tbl.code).subscribe(rst => this.tableTemplates = rst, err => this.handleError(err));
	}
	selectTemplate(tmpl: TableTemplate): void {
		if (this.selectedTemplate == tmpl) {
			this.selectedTemplate = null;
			this.newTemplate = new TableTemplate();
		} else {
			this.selectedTemplate = tmpl;
			this.newTemplate = tmpl;
		}
	}
	updateTemplate(): void {
		this.dataieService.updateTableTemplate(this.selectedTemplate).subscribe(() => {
			this.showSuccessKey("msg.saved.success");
		}, err => this.handleError(err));
	}
	delTemplate(id: number): void {
		this.dataieService.delTemplate(id).subscribe(() => {
			this.tableTemplates = this.tableTemplates.filter(t => t.id !== id);
			this.selectedTable = null; 
		}, err => this.handleError(err));
	}
	downDefaultTableTemplate(): void {
		this.dataieService.genDefaultTableTemplate(this.selectedTable).subscribe(rst => {
			window.open(fileDownloadRoot + rst.fileUri, "_blank", "top=100,left=100,width=100,height=100,menubar=no,scrollbars=no,toolbar=no,status=yes");
		}, err => this.handleError(err));
	}
	saveTemplate(): void {
		if (!this.newTemplate.dscName) {
			this.showWarnKey("txt.PleaseInputTemplateDescription");
		} else if (!this.tpltFileUploadInfo.uploadedFiles) {
			this.showWarnKey("txt.waitForUploading");
		} else if (this.selectedTemplate == this.newTemplate || (this.tpltFileUploadInfo.uploadedFiles && this.tpltFileUploadInfo.uploadedFiles.length > 0)) {
			if (this.tpltFileUploadInfo.uploadedFiles && this.tpltFileUploadInfo.uploadedFiles.length > 0) {
				this.newTemplate.fileUri = this.tpltFileUploadInfo.uploadedFiles[this.tpltFileUploadInfo.uploadedFiles.length-1];
			}
			this.dataieService.newTableTemplate(this.newTemplate).subscribe(rst => {
				this.tpltFileUploadInfo.reset();
				if (this.selectedTemplate) {
					this.selectedTemplate = null;
				} else {
					this.tableTemplates.push(rst);
				}
				this.newTemplate = new TableTemplate();
				this.showSuccessKey("msg.saved.success");
			}, err => this.handleError(err));
		} else {
			this.showWarnKey("txt.PleaseSelectTemplateToUpload");
		}
	}
}